<?php
/*
 * 评价--回复类
 * 
 */
namespace Home\Logic;

class EvaluateLogic{

	public function __construct() {
        $this->e = D('Evaluate');
        
    }

    public function setStoreId($store_id){
        if($store_id<1){
            throw new \Exception('store_id is null');
        }       
        $this->e->setTableNamePre($store_id);
    }

    public function insertEvaluate($data){
        $this->setStoreId($data['store_id']);
    	return $this->e->add($data);
    }

    //商品评分
    public function score($item_number,$store_id){
        $this->setStoreId($store_id);
        $re = $this->e->where("item_number='$item_number' " )->order('id desc')->select();
        return $re;
    } 

    //商品详情的评价
    public function getEvaluate($item_number,$store_id){
        $this->setStoreId($store_id);
        $re = $this->e->where("item_number='$item_number' and status != 0 " )->order('id desc')->find();
        if($re['evaluate_id'] != 0){
                $re['relpy'] = $this->relpy($re['evaluate_id'],$re['store_id']);
        }

        return $re;
    }

    // 更多评价
    public function getMoreEvaluate($item_number,$store_id,$start,$length=12){
        $this->setStoreId($store_id);
        $re = $this->e->where("item_number='$item_number' and status != 0 " )->order('id desc')->limit($start,$length)->select();
        foreach($re as $k=>$v){
            if($v['evaluate_id'] != 0){
                $re[$k]['relpy'] = $this->relpy($v['evaluate_id'],$v['store_id']);
                $evaluate_ids[]= $v['evaluate_id'];
            }

            if(in_array($v['id'], $evaluate_ids)){
                unset($re[$k]);
            }
        }
        
        return $re;
    } 


    //回复评价
    public function relpy($evaluate_id,$store_id){
        $this->setStoreId($store_id);
        $re = $this->e->where('id='.$evaluate_id)->find();
        return $re['content'];
    }

    //C端未读评价数量
    public function unReadCount($store_id,$client_id){
        $this->setStoreId($store_id);
        $re = $this->e->where('store_id='.$store_id.' and client_id='.$client_id.' and status=1 and evaluate_id != 0')->count();
        return $re;
    }

    //C端未读评价
    public function unRead($store_id,$client_id,$start=0,$length=12){
        $this->setStoreId($store_id);
        $this->updateRead($store_id,$client_id);
        $re = $this->e->where('store_id='.$store_id.' and client_id='.$client_id.' and evaluate_id != 0')->order('id desc')->limit($start,$length)->select();
        foreach($re as $k=>$v){
            $re[$k]['relpy'] = $this->relpy($v['evaluate_id'],$store_id);
            $evaluate_ids[]= $v['evaluate_id'];

            if(in_array($v['id'], $evaluate_ids)){
                unset($re[$k]);
            }
        }
        return $re;
    }

    //未读状态改变
    private function updateRead($store_id,$client_id){
        $this->setStoreId($store_id);
        $data = array('status'=>2);
        //C端
        $re = $this->e->where('store_id='.$store_id.' and client_id='.$client_id.' and evaluate_id != 0')->save($data);
        
    }

    //
    public function getEvaluateByOrdernumber($store_id,$order_number,$client_id,$item_id){
        $this->setStoreId($store_id);
        return $re = $this->e->where('store_id='.$store_id.' and client_id='.$client_id.' and item_id='.$item_id.' and order_number='.$order_number)->find();
    }

    // b端未读状态改变
    public function b_updateRead($item_number,$store_id){
        $this->setStoreId($store_id);
        $data = array('status'=>2);
        $re = $this->e->where('store_id='.$store_id." and item_number='$item_number' and evaluate_id = 0")->save($data);
    }

    public function checkEvaluate($item_number,$evaluate_id,$order_number,$store_id){
        $this->setStoreId($store_id);
        return $this->e->where('store_id='.$store_id." and item_number='$item_number' and order_number=order_number and evaluate_id = $evaluate_id")->find();
    }

    public function appendEvaluate($item_number,$store_id,$start,$length=12){
        $this->setStoreId($store_id);
        $re = $this->e->where("item_number='$item_number' and store_id=$store_id and status != 0 " )->order('id desc')->limit($start,$length)->select();
        foreach($re as $k=>$v){
            if($v['evaluate_id'] != 0){
                $re[$k]['relpy'] = $this->relpy($v['evaluate_id'],$store_id);
                $evaluate_ids[]= $v['evaluate_id'];
            }

            if(in_array($v['id'], $evaluate_ids)){
                unset($re[$k]);
            }
        }  
        return $re;
    }

}